package com.goktech.controller;

import com.goktech.service.*;
import com.goktech.service.impl.Userserviceimpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(value = "/UserServlet")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String sc = request.getParameter("sc");
        if(sc.contains("reg")){
            String password = request.getParameter("password");
            String username = request.getParameter("username");
            String flag = request.getParameter("flag");
            String score = request.getParameter("score");
            System.out.println("password: " + password);
            System.out.println("username: " + username);
            System.out.println("flag: " + flag);
            System.out.println("score: " + score);
            Userservice service=new Userserviceimpl();
            service.register(username, password,flag,score);
            RequestDispatcher dispatcher = request.getRequestDispatcher("login.jsp");
            dispatcher.forward(request, response);
        }
        if(sc.equals("login")){
            String password = request.getParameter("password");
            String username = request.getParameter("username");
            Userservice service=new Userserviceimpl();
            boolean login = service.login(username, password);
            System.out.println(login);
            if(login){
                RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp");
                dispatcher.forward(request, response);
            }
            else{
                response.sendRedirect("login.jsp");
            }
        }

    }
}